#ifndef RECTANGLE_H
#define RECTANGLE_H

#endif // RECTANGLE_H
#pragma once
#include<vector>
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
//矩形类
class rectangle {
protected:
    rectangle() {}
public:
    void delayConstru(int x1, int y1, int x2, int y2)
    {
        width = x2 - x1;
        height = y2 - y1;

        area = width * height;

        x_mid = float(x1 + x2) / 2.0;
        y_mid = float(y2 + y1) / 2.0;

        up = false;
        right = false;

        this->x1 = x1;
        this->y1 = y1;
        this->x2 = x2;
        this->y2 = y2;



    }


    string init_inf;
    //矩形编号，和信息素表的顺序相同,主函数初始化的时候获得
    int index = -1;

    //长宽
    int width = 0;
    int height = 0;

    //面积
    int area;

    //位置信息
    int x1 = 0;
    int y1 = 0;
    int x2 = 0;
    int y2 = 0;
    float x_mid = 0;//矩形中心点坐标
    float y_mid = 0;


    //上右摆放状态
    bool up = false;
    bool right = false;

    //记录该矩形是否被摆放
    bool flag = false;//否

    //该矩形的左右孩子节点，左节点对应上，右节点对应右
    rectangle *r_left = NULL;
    rectangle *r_right = NULL;


    bool operator ==(const rectangle& p) const {
        if (p.x1 == this->x1&&p.y1 == this->y1&&p.x2 == this->x2&&p.y2 == this->y2) {
            return true;
        }
        return false;
    }

    rectangle(int x1, int y1, int x2, int y2)
    {
        this->delayConstru(x1, y1, x2, y2);
    }
    rectangle(const rectangle &r)//实现拷贝构造
    {
        this->index = r.index;
        this->delayConstru(r.x1, r.y1, r.x2, r.y2);
        this->init_inf=r.init_inf;
        //cout << "拷贝调用" <<this->index<< endl;
        /*this->x1 = r.x1;
        this->y1 = r.y1;
        this->x2 = r.x2;
        this->y2 = r.y2;
        this->width = this->x2 - this->x1;
        this->height = this->y2 - this->y1;
        this->x_mid=(this->x1+this)*/
    }
    //进入槽子，被拼到当前矩形上部，修改信息
    void set_up(rectangle *r_now)
    {
        this->x1 = (*r_now).x1;
        this->y1 = (*r_now).y1 + (*r_now).height;
        this->x2 = this->x1 + this->width;
        this->y2 = this->y1 + this->height;

        this->x_mid = this->x1 + (this->width) / 2.0;
        this->y_mid = this->y1 + (this->height) / 2.0;
    }

    //进入槽子，被拼到当前矩形右部，修改信息
    void set_right(rectangle *r_now)
    {

        this->x1 = (*r_now).x1 + (*r_now).width;
        this->y1 = (*r_now).y1;
        this->x2 = this->x1 + this->width;
        this->y2 = this->y1 + this->height;

        this->x_mid = this->x1 + (this->width) / 2.0;
        this->y_mid = this->y1 + (this->height) / 2.0;
    }

    void to_String()
    {
        cout << "编号 " << index << endl;
        cout << "width " << width << endl;
        cout << "height " << height << endl;
        cout << "x1 " << x1 << endl;
        cout << "y1 " << y1 << endl;
        cout << "x2 " << x2 << endl;
        cout << "y2 " << y2 << endl;
        cout << "x_mid " << x_mid << endl;
        cout << "y_mid " << y_mid << endl;
        cout << "up " << up << endl;
        cout << "right " << right << endl;
        cout << "flag " << flag << endl;
        cout << endl;
        //cout << "*r_left" << r_left->index << endl;
        //cout << "*r_right" << r_right->index << endl;

    }

    virtual void write_file(fstream& file) {
        string result;
        result += "In Polygon:\n";
        result+=this->init_inf+"\n";
        result += "Out Polygon:\n";
        result += "(" + to_string(x1) + "," + to_string(y1) + ")";
        result += "(" + to_string(x2) + "," + to_string(y1) + ")";
        result += "(" + to_string(x2) + "," + to_string(y2) + ")";
        result += "(" + to_string(x1) + "," + to_string(y2) + ")" + "\n";
        file << result;
    }
    virtual int get_occu_area() {
        return this->area;
    }
    virtual rectangle* get_copy() {
        return new rectangle(*this);
    }
    virtual void do_process(){

    }
};
